<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container">
    <nav class="tab">
      <a
        @click="comName = 'CallbackBind'"
        href="javascript:;"
        :class="{ active: comName === 'CallbackBind' }"
      >
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a
        @click="comName = 'CallbackPatch'"
        href="javascript:;"
        :class="{ active: comName === 'CallbackPatch' }"
      >
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div>
      <Component :is="comName" :openId="qqOpenId" />
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import LoginHeader from './components/login-header'
import LoginFooter from './components/login-footer'
import CallbackBind from './components/callback-bind'
import CallbackPatch from './components/callback-patch'
import { ref } from 'vue'
import QC from 'qc'
import { reqUserQQLogin } from '@/api/user'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { message } from '@/components/Message/index.js'

export default {
  name: 'PageCallback',
  components: { LoginHeader, LoginFooter, CallbackBind, CallbackPatch },
  setup () {
    const comName = ref('CallbackBind')
    const qqOpenId = ref('')

    // 已注册已绑定
    const store = useStore()
    const router = useRouter()
    if (QC.Login.check()) {
      // 检查QQ是否登录
      QC.Login.getMe(async openId => {
        qqOpenId.value = openId

        const res = await reqUserQQLogin(openId)
        console.log('---res---', res)
        // 提示
        message({ type: 'success', text: 'QQ登录成功' })
        // 把信息保存到vuex中
        store.commit('user/setProfile', res.result)
        // 已注册已绑定
        router.push('/')
      })
    }

    return {
      comName,
      qqOpenId
    }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
